package cn.smile.business.basic.impl;

import cn.smile.bean.entity.basic.VoucherItem;
import cn.smile.business.basic.IVoucherItemService;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.repository.basic.VoucherItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
public class VoucherItemServiceImpl extends BaseServiceImpl<VoucherItemMapper, VoucherItem> implements IVoucherItemService {
    @Override
    public void saveEntity(String voucherId, List<String> itemList) {
        log.info("[VoucherItemServiceImpl].[saveEntity] ------> 保存优惠券关联关系 Start");
        //删除之前的
        this.delByVoucherId(voucherId);
        if (!CollectionUtils.isEmpty(itemList)) {
            for (String itemId : itemList) {
                VoucherItem voucherItem = new VoucherItem();
                voucherItem.setVoucherId(voucherId);
                voucherItem.setItemId(itemId);
                super.create(voucherItem);
            }
        }
        log.info("[VoucherItemServiceImpl].[saveEntity] ------> 保存优惠券关联关系 End");
    }

    @Override
    public Map<String, List<String>> getItemIdsMap(List<String> voucherIds) {
        log.info("[VoucherItemServiceImpl].[getItemIdsMap] ------> 查询代金券关联关系集合 Start");
        Map<String, List<String>> dataMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(voucherIds)) {
            List<VoucherItem> voucherItems = baseMapper.selectList(new QueryWrapper<VoucherItem>().lambda().in(VoucherItem::getVoucherId, voucherIds).eq(VoucherItem::getDeleted, Boolean.FALSE));
            if (!CollectionUtils.isEmpty(voucherItems)) {
                for (VoucherItem entity : voucherItems) {
                    if (!dataMap.containsKey(entity.getVoucherId())) {
                        dataMap.put(entity.getVoucherId(), Lists.newArrayList());
                    }
                    dataMap.get(entity.getVoucherId()).add(entity.getItemId());
                }
            }
        }
        log.info("[VoucherItemServiceImpl].[getItemIdsMap] ------> 查询代金券关联关系集合 End");
        return dataMap;
    }

    @Override
    public void delByVoucherId(String voucherId) {
        log.info("[VoucherItemServiceImpl].[delByVoucherId] ------> 根据代金券ID删除关联关系 Start, voucherId = {}", voucherId);
        List<VoucherItem> voucherItems = baseMapper.selectList(new QueryWrapper<VoucherItem>().lambda().eq(VoucherItem::getVoucherId, voucherId).eq(VoucherItem::getDeleted, Boolean.FALSE));
        if (!CollectionUtils.isEmpty(voucherItems)) {
            voucherItems.forEach(voucherItem -> super.delById(voucherItem.getId()));
        }
        log.info("[VoucherItemServiceImpl].[delByVoucherId] ------> 根据代金券ID删除关联关系 End");
    }

    @Override
    public List<String> getVoucherIdByItemList(List<String> itemIds) {
        log.info("[VoucherItemServiceImpl].[getVoucherIdByItemList] ------> 根据消费单ID集合查询可用代金券ID集合 Start");
        List<VoucherItem> voucherItems = baseMapper.selectList(new QueryWrapper<VoucherItem>().lambda().in(VoucherItem::getItemId, itemIds).eq(VoucherItem::getDeleted, Boolean.FALSE));
        List<String> voucherIds = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(voucherItems)) {
            for (VoucherItem data : voucherItems) {
                voucherIds.add(data.getVoucherId());
            }
        } else {
            voucherIds.add(UUID.randomUUID().toString());
        }
        log.info("[VoucherItemServiceImpl].[getVoucherIdByItemList] ------> 根据消费单ID集合查询可用代金券ID集合 End");
        return voucherIds;
    }
}
